(function(){
  $.getScript("http://wsyc.lqwang.com/Scripts/ych21.js");
})()

/**
 * @param orderData 预约日期 YYYYMMDD
 * @param timeSpan 预约时段 
 *  -1 全天 811 8:00~11:00 1215 12:00~15:00 
 *  1518 15:15~18:15 1820 18:30~20:45
 * 
 * e.g:
 *  预定5月20号上午8点~11点的车
 *  Ordering(“20160520”， “811”)；
 * 
 *  预定5月20号全天的车
 *  Ordering(“20160520”， “-1”)；
 * 
 */
var orderCar = function(orderDate, timeSpan) {
  console.log("正在预约" + orderDate + " " + timeSpan + "...");
  var condition = {};
  condition["yyrq"] = orderDate;
  
  condition["xnsd"] = timeSpan;
  
  /**
   * 训练类型
   * 1 原地训练
   * 3 道路训练
   * 4 实际道路
   */
  condition["xllxid"] = 3;
  
  // 科目二练习
  condition["type"] = "km2Car";
  condition["orderby"] = "";

  // 场内编号，可留空
  condition["cnbh"] = "";
  
  var queryCondition = {}
  queryCondition.filters = condition;
  queryCondition.orderBy = "";
  queryCondition.pageno = 1;
  queryCondition.pagesize = 1000;
  
  // 进行查询
  $.ajax({
    url: 'Tools/km2.aspx?date=' + new Date(),
    data: queryCondition,
    cache: false,
    success: function (data) {
      if (data.length == 0 || data == "null_0" || data.indexOf("错误页") > -1) {
        console.log("没有符合条件的预定车辆...");
        return;
      }  
      
      /**
       * data sample
       * 
[{
  "JLCBH": "611059",
  "CNBH": "17003",
  "YT": null,
  "LXBH": null,
  "JLYXM": "孙俊伍",
  "XNSD": "15:15-18:15.1518.4.-1,",
  "TCFW": "",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611061",
  "CNBH": "17013",
  "YT": null,
  "LXBH": null,
  "JLYXM": "闫海涛",
  "XNSD": "12:00-15:00.1215.4.-1,",
  "TCFW": "第一停车场",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611063",
  "CNBH": "17007",
  "YT": null,
  "LXBH": null,
  "JLYXM": "苑连强",
  "XNSD": "12:00-15:00.1215.4.-1,",
  "TCFW": "",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611073",
  "CNBH": "17017",
  "YT": null,
  "LXBH": null,
  "JLYXM": "赵淼",
  "XNSD": "08:00-11:00.811.4.-1,15:15-18:15.1518.4.-1,",
  "TCFW": "",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611074",
  "CNBH": "17018",
  "YT": null,
  "LXBH": null,
  "JLYXM": "张雅林",
  "XNSD": "12:00-15:00.1215.4.-1,",
  "TCFW": "第一停车场",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611077",
  "CNBH": "17021",
  "YT": null,
  "LXBH": null,
  "JLYXM": "李建国",
  "XNSD": "15:15-18:15.1518.4.-1,",
  "TCFW": "第一停车场",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611078",
  "CNBH": "17022",
  "YT": null,
  "LXBH": null,
  "JLYXM": "董玉军",
  "XNSD": "08:00-11:00.811.4.-1,",
  "TCFW": "",
  "PHOTOPATH": ""
},
{
  "JLCBH": "611079",
  "CNBH": "17023",
  "YT": null,
  "LXBH": null,
  "JLYXM": "刘常明",
  "XNSD": "12:00-15:00.1215.4.-1,",
  "TCFW": "第一停车场",
  "PHOTOPATH": ""
}
]
       */
      //test
      var data_ = [{
        "JLCBH": "611059",
        "CNBH": "17003",
        "YT": null,
        "LXBH": null,
        "JLYXM": "孙俊伍",
        "XNSD": "15:15-18:15.1518.4.-1,",
        "TCFW": "",
        "PHOTOPATH": ""
      },
      {
        "JLCBH": "611061",
        "CNBH": "17013",
        "YT": null,
        "LXBH": null,
        "JLYXM": "闫海涛",
        "XNSD": "12:00-15:00.1215.4.-1,",
        "TCFW": "第一停车场",
        "PHOTOPATH": ""
      }];
            
      // 进行预约
      for (var i = 0; i < data.length; ++i) {
        var item = data[i];
        var xnsds = item["XNSD"].split(',');
        for (var i = 0; i < xnsds.length; i++) {
            var xnsd = xnsds[i];
            if (xnsd != null && xnsd != undefined && xnsd != "") {
              var info = xnsd.split('.');
              var sdname = info[0];
              var sdid = info[1];
              var sl = info[2];
              var jcsd = info[3];
              makeOrder(item["JLCBH"], orderDate, sdid, 3, jcsd);
            }
        }
      }
    }
  });
}

/**
 * @param trainType_
 * 训练类型
 *  1 原地训练
 *  3 道路训练
 *  4 实际道路
*/
            
var makeOrder = function(jlcbh_, yyrq_, xnsd_, trainType_, jcsd_) {
  $.ajax({
          url: 'Tools/km2.aspx',  
          data: { 
            jlcbh: jlcbh_, 
            yyrqbegin: yyrq_, 
            xnsd: xnsd_, 
            
            trainType: trainType_, 
            type: "km2Car2", 
            jcsd: jcsd_,
          },
          cache: false,
          success: function (data) {
            console.log(data);
          }
       });
}